package com.cskaoyan.controller;

import com.cskaoyan.vo.BaseRespVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author stone
 * @date 2022/10/06 11:39
 */
@Controller
public class HelloController {

    //localhost:8080/hello1?username=songge   👉 hello songge
    @RequestMapping("hello1")
    public ModelAndView hello1(HttpServletRequest request) {
        String username = request.getParameter("username");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/hello.jsp");//相对于web应用的根路径 相对路径
        modelAndView.addObject("usernamez", username);
        return modelAndView;
    }

    //localhost:8080/hello2?username=ligenli   👉 hello ligenli
    @RequestMapping("hello2")
    public String hello2(Model model, HttpServletRequest request) {

        String username = request.getParameter("username");
        model.addAttribute("usernamez", username);
        return "/WEB-INF/hello.jsp";
        // 利用String返回值会封装为ModelAndView，String作为其视图名
    }

    @RequestMapping("hello3")
    @ResponseBody
    public BaseRespVo hello3() {
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setMsg("一切都好");
        baseRespVo.setStatus(200);
        return baseRespVo;
    }
}
